package com.pubinfo.passbook.common.utils.tax;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.tuple.ImmutablePair;
import org.springframework.util.CollectionUtils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Slf4j
public class Util {

//  public static void main(String[] args) {
//
//   // List<ImmutablePair<String, String>> pairList = getFinancialTwoDaysDay("20210601", "20210630");
//    System.out.println(getFinancialOnTheDay());
//  }
//
//取当天
public static String getFinancialOnTheDay() {
  SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
  Calendar calendar = Calendar.getInstance();
  // calendar.add(Calendar.DATE, -1);
  
  return sdf.format(calendar.getTime());
}
  
  public static String getTaxOnTheDay() {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    Calendar calendar = Calendar.getInstance();
    calendar.add(Calendar.DATE, -1);
    
    return sdf.format(calendar.getTime());
  }//取一段时间的每一天
  
  public static List<ImmutablePair<String, String>> getFinancialTwoDaysDay(String dateStart, String dateEnd) {
  
    List<String> listDate = getTwoDaysDay(dateStart, dateEnd, "yyyyMMdd");
    List<ImmutablePair<String, String>> pairList = new ArrayList<>();
    //按月进行分组
  
    if (!CollectionUtils.isEmpty(listDate)) {
      for (int i = 0; i < listDate.size(); i++) {
        if (i % 7 == 0) {
          ImmutablePair<String, String> pair = null;
          if (i + 6 < listDate.size()) {
            //可以整除
            pair = ImmutablePair.of(listDate.get(i), listDate.get(i + 6));
          } else {
            pair = ImmutablePair.of(listDate.get(i), listDate.get(listDate.size() - 1));
          }
          pairList.add(pair);
        }
      }
    }
    return pairList;
  }
  
  //取一段时间的每一天
  public static List<String> getTaxTwoDaysDay(String dateStart, String dateEnd) {
    return getTwoDaysDay(dateStart, dateEnd, "yyyy-MM-dd");
  }
  
  //取一段时间的每一天
  public static List<String> getTwoDaysDay(String dateStart, String dateEnd, String format) {
    SimpleDateFormat sdf = new SimpleDateFormat(format);
    List<String> dateList = new ArrayList<String>();
    try {
      Date dateOne = sdf.parse(dateStart);
      Date dateTwo = sdf.parse(dateEnd);
      Calendar calendar = Calendar.getInstance();
      calendar.setTime(dateOne);
      dateList.add(dateStart);
      while (calendar.getTime().before(dateTwo)) { //倒序时间,顺序after改before其他相应的改动。
        calendar.add(Calendar.DAY_OF_MONTH, +1);
        dateList.add(sdf.format(calendar.getTime()));
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
    return dateList;
  }
  
}
